package com.metagurukul.informer.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;

/**
 * Servlet Filter implementation class CapcthaFilter
 */
@WebFilter(urlPatterns = { "/CapcthaFilter" }, servletNames = { "AskQuestion" })
public class CaptchaFilter implements Filter {

   
	public void destroy() {
		// TODO Auto-generated method stub
	}


	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpSession session=((HttpServletRequest) request).getSession();
		String captcha = (String)session.getAttribute("captcha");
	    String answer = request.getParameter("answer");
	    if(captcha.equals(answer))
	    	chain.doFilter(request, response);
	    else{
	    	session.setAttribute("captcha_status", false);
	    	((HttpServletResponse)response).sendRedirect("AskQuestion.jsp");
	    }
	}

	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
